/* eslint-disable*/

import Vue from 'vue'
import axios from 'axios'
import VueAxios from 'vue-axios'
import qs from 'qs'
import {
  LoadingPlugin
} from 'vux'


import {
  AlertPlugin
} from 'vux'


Vue.use(VueAxios, axios, LoadingPlugin, AlertPlugin)
import Bus from '@/common/js/bus.js'

import {
  Loading,
  Message
} from 'element-ui'
import {
  cookie
} from 'vux'

import {
  indexedDB
} from "@/api/indexedDB.js";

//import {debounce} from 'vux'

import {
  isIdNumber,
  randomNumber,
  dateStringToDateTime
} from "@/common/js/main.js";


var close = function () {
  loadinginstace.close();
}

function debounce(method, delay, context) {
  clearTimeout(method.timer);
  method.timer = setTimeout(function () {
    method.call(context);
  }, delay);
}

// 超时时间
//axios.defaults.timeout = 5000
// http请求拦截器
var loadinginstace


function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms))
}

var data;
//http://r3.ihuipao.cn/race_config/delay3.json
//http://p392e86hy.bkt.clouddn.com/delay3.json

axios.get('https://r3.ihuipao.cn/wuxih5/delay.json?' + Math.random()).then(response => {
  //console.log('---')
  //console.log(response.data)
  data = response.data
  //console.log(data)
  //console.log(window.pageConfig.raceid)
}, error => {
  console.log(error)
})




axios.interceptors.request.use(async (config) => {
  loadinginstace = Loading.service({
    fullscreen: true
  })
  let lang = document.querySelector('html').lang || 'zh-cn'




  // if (typeof(config.params) == 'undefined') {
  //   config.data = `${config.data}&_sid=${cookie.get('_sid')}&_lang=${lang}&_channel=${cookie.get('_channel')}`
  // } else {
  //   config.params._channel = cookie.get('_channel')
  //   config.params._sid = cookie.get('_sid')
  //   config.params._lang = lang
  // }




  if (typeof (config.params) == 'undefined') {
    //console.log(config.params)
    

    //console.log(config.data)
    //这是post方法
    if (window.localStorage.getItem('session_sid') || cookie.get('_sid')) {
      //如果有_sid
      config.data = `${config.data}&_sid=${cookie.get('_sid')}&_lang=${lang}&_channel=${cookie.get('_channel')}&_from=${cookie.get('_from')}`
      //config.data = `${config.data}&_lang=${lang}&_channel=${cookie.get('_channel')}&_from=${cookie.get('_from')}`
    } else {
      config.data = `${config.data}&_lang=${lang}&_channel=${cookie.get('_channel')}&_from=${cookie.get('_from')}`
    }
    //console.log(config.data)
  } else {

    //这是get方法
    if (window.localStorage.getItem('session_sid') || cookie.get('_sid')) {
      //如果有_sid
      config.params._channel = cookie.get('_channel')
      config.params._sid = cookie.get('_sid')
      config.params._lang = lang
      config.params._from = cookie.get('_from')
    } else {
      config.params._channel = cookie.get('_channel')
      config.params._lang = lang
      config.params._from = cookie.get('_from')
    }

  }




  let isSleep = false
  let sleepMin = 0
  let sleepMax = 0


  //console.log(haha)
  //console.warn(data)
  if (!!data) {

    data.forEach(async (item) => {
      
      if (item.raceid == window.pageConfig.raceid) {
    
        

        let startTime = dateStringToDateTime(item.time[0]) || null
        let endTime = dateStringToDateTime(item.time[1]) || null
        let timeDate = new Date().valueOf()

     
        
        if (timeDate >= startTime && timeDate <= endTime) {
          
          isSleep = true
          sleepMin = item.delay[0]
          sleepMax = item.delay[1]

        }
      }
    })
  }


  if (isSleep) {
    
    await sleep(randomNumber(sleepMin, sleepMax))
  }




  return config
}, error => {
  loadinginstace.close()
  Message.error({
    message: '加载超时'
  })
  return Promise.reject(error)
})


// http响应拦截器
axios.interceptors.response.use(data => { // 响应成功关闭loading
  //防抖函数
  debounce(close, 500);

  //获取eneventlist 测试用
  //console.log(data.data.data.length)


  // if (data.data.data.length === 4) {
  //   Bus.$emit("showLogin", data.data.code)
  // }



  if (data.data.code == '401'||data.data.code == '403') {
    //401未登录 403没权限
    
    Bus.$emit("showLogin", data.data)
    cookie.remove('_sid')
    //Bus.$emit("close123",'1111');
    // indexedDB('IDNumber', 'remove', window.pageConfig.RACEID, '', function (a) {

    // }.bind(this));
    //let reg=/#/$/
    //if(window.location.href){

    //}

    //Vue.$router.push({path:'./'})

    // window.location.href = '//' + window.location.host + '/baoming/'+window.pageConfig.raceid+'#/'
  }
  return data
}, error => {
  console.log(error)
  loadinginstace.close()
  Message.error({
    message: '加载失败'
  })
  return Promise.reject(error)
})


export {
  axios
}
